package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.KfnWorkOrderBo;
import com.ruoyi.system.domain.vo.KfnWorkOrderVo;
import com.ruoyi.system.domain.KfnWorkOrder;
import com.ruoyi.system.mapper.KfnWorkOrderMapper;
import com.ruoyi.system.service.IKfnWorkOrderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工单池Service业务层处理
 *
 * @author Panda
 * @date 2023-12-20
 */
@RequiredArgsConstructor
@Service
public class KfnWorkOrderServiceImpl implements IKfnWorkOrderService {

    private final KfnWorkOrderMapper baseMapper;

    /**
     * 查询工单池
     */
    @Override
    public KfnWorkOrderVo queryById(String workOrderId){
        return baseMapper.selectVoById(workOrderId);
    }

    /**
     * 查询工单池列表
     */
    @Override
    public TableDataInfo<KfnWorkOrderVo> queryPageList(KfnWorkOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KfnWorkOrder> lqw = buildQueryWrapper(bo);
        Page<KfnWorkOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工单池列表
     */
    @Override
    public List<KfnWorkOrderVo> queryList(KfnWorkOrderBo bo) {
        LambdaQueryWrapper<KfnWorkOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KfnWorkOrder> buildQueryWrapper(KfnWorkOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KfnWorkOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWorkOrderType()), KfnWorkOrder::getWorkOrderType, bo.getWorkOrderType());
        lqw.eq(StringUtils.isNotBlank(bo.getThirdSysNumber()), KfnWorkOrder::getThirdSysNumber, bo.getThirdSysNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getContacts()), KfnWorkOrder::getContacts, bo.getContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getContactNumber()), KfnWorkOrder::getContactNumber, bo.getContactNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreId()), KfnWorkOrder::getStoreId, bo.getStoreId());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreNumber()), KfnWorkOrder::getStoreNumber, bo.getStoreNumber());
        lqw.like(StringUtils.isNotBlank(bo.getStoreName()), KfnWorkOrder::getStoreName, bo.getStoreName());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreType()), KfnWorkOrder::getStoreType, bo.getStoreType());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreAddress()), KfnWorkOrder::getStoreAddress, bo.getStoreAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkOrderDescription()), KfnWorkOrder::getWorkOrderDescription, bo.getWorkOrderDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkOrderStatusId()), KfnWorkOrder::getWorkOrderStatusId, bo.getWorkOrderStatusId());
        lqw.eq(bo.getWorkOrderResponseTimeOver() != null, KfnWorkOrder::getWorkOrderResponseTimeOver, bo.getWorkOrderResponseTimeOver());
        lqw.eq(bo.getWorkOrderOnsiteTimeOver() != null, KfnWorkOrder::getWorkOrderOnsiteTimeOver, bo.getWorkOrderOnsiteTimeOver());
        lqw.eq(bo.getWorkOrderProcessingTimeOver() != null, KfnWorkOrder::getWorkOrderProcessingTimeOver, bo.getWorkOrderProcessingTimeOver());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectId()), KfnWorkOrder::getProjectId, bo.getProjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectNumber()), KfnWorkOrder::getProjectNumber, bo.getProjectNumber());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), KfnWorkOrder::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkOrderStatusIdChild()), KfnWorkOrder::getWorkOrderStatusIdChild, bo.getWorkOrderStatusIdChild());
        return lqw;
    }

    /**
     * 新增工单池
     */
    @Override
    public Boolean insertByBo(KfnWorkOrderBo bo) {
        KfnWorkOrder add = BeanUtil.toBean(bo, KfnWorkOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setWorkOrderId(add.getWorkOrderId());
        }
        return flag;
    }

    /**
     * 修改工单池
     */
    @Override
    public Boolean updateByBo(KfnWorkOrderBo bo) {
        KfnWorkOrder update = BeanUtil.toBean(bo, KfnWorkOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KfnWorkOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工单池
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
